home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / ziodevs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.8 KB  |  262 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: ziodevs.c,v 1.2 2000/09/19 19:00:54 lpd Exp $ */
  20. /* %stdxxx IODevice implementation for PostScript interpreter */
  21. #include "stdio_.h"
  22. #include "ghost.h"
  23. #include "gpcheck.h"
  24. #include "oper.h"
  25. #include "stream.h"
  26. #include "gxiodev.h"        /* must come after stream.h */
  27.                 /* and before files.h */
  28. #include "files.h"
  29. #include "store.h"
  30.  
  31. /* Define the special devices. */
  32. const char iodev_dtype_stdio[] = "Special";
  33. #define iodev_special(dname, init, open) {\
  34.     dname, iodev_dtype_stdio,\
  35.     { init, open, iodev_no_open_file, iodev_no_fopen, iodev_no_fclose,\
  36.       iodev_no_delete_file, iodev_no_rename_file, iodev_no_file_status,\
  37.       iodev_no_enumerate_files, NULL, NULL,\
  38.       iodev_no_get_params, iodev_no_put_params\
  39.     }\
  40. }
  41.  
  42. /*
  43.  * We need the current context pointer for accessing / opening the %std
  44.  * IODevices.  However, this is not available to the open routine.
  45.  * Therefore, we use the hack of storing this pointer in the IODevice state
  46.  * pointer just before calling the open routines.  We clear the pointer
  47.  * immediately afterwards so as not to wind up with dangling references.
  48.  */
  49.  
  50. #define STDIN_BUF_SIZE 128
  51. /*#define ref_stdin ref_stdio[0] *//* in files.h */
  52. bool gs_stdin_is_interactive;    /* exported for command line only */
  53. private iodev_proc_init(stdin_init);
  54. private iodev_proc_open_device(stdin_open);
  55. const gx_io_device gs_iodev_stdin =
  56.     iodev_special("%stdin%", stdin_init, stdin_open);
  57.  
  58. #define STDOUT_BUF_SIZE 128
  59. /*#define ref_stdout ref_stdio[1] *//* in files.h */
  60. private iodev_proc_open_device(stdout_open);
  61. const gx_io_device gs_iodev_stdout =
  62.     iodev_special("%stdout%", iodev_no_init, stdout_open);
  63.  
  64. #define STDERR_BUF_SIZE 128
  65. /*#define ref_stderr ref_stdio[2] *//* in files.h */
  66. private iodev_proc_open_device(stderr_open);
  67. const gx_io_device gs_iodev_stderr =
  68.     iodev_special("%stderr%", iodev_no_init, stderr_open);
  69.  
  70. /* ------- %stdin, %stdout, and %stderr ------ */
  71.  
  72. /*
  73.  * According to Adobe, it is legal to close the %std... files and then
  74.  * re-open them later.  However, the re-opened file object is not 'eq' to
  75.  * the original file object (in our implementation, it has a different
  76.  * read_id or write_id).
  77.  */
  78.  
  79. private int
  80.     s_stdin_read_process(P4(stream_state *, stream_cursor_read *,
  81.                 stream_cursor_write *, bool));
  82.  
  83. private int
  84. stdin_init(gx_io_device * iodev, gs_memory_t * mem)
  85. {
  86.     gs_stdin_is_interactive = true;
  87.     return 0;
  88. }
  89.  
  90. /* Read from stdin into the buffer. */
  91. /* If interactive, only read one character. */
  92. private int
  93. s_stdin_read_process(stream_state * st, stream_cursor_read * ignore_pr,
  94.              stream_cursor_write * pw, bool last)
  95. {
  96.     FILE *file = ((stream *) st)->file;        /* hack for file streams */
  97.     int wcount = (int)(pw->limit - pw->ptr);
  98.     int count;
  99.  
  100.     if (wcount > 0) {
  101.     if (gs_stdin_is_interactive)
  102.         wcount = 1;
  103.     count = fread(pw->ptr + 1, 1, wcount, file);
  104.     if (count < 0)
  105.         count = 0;
  106.     pw->ptr += count;
  107.     } else
  108.     count = 0;        /* return 1 if no error/EOF */
  109.     process_interrupts();
  110.     return (ferror(file) ? ERRC : feof(file) ? EOFC : count == wcount ? 1 : 0);
  111. }
  112.  
  113. private int
  114. stdin_open(gx_io_device * iodev, const char *access, stream ** ps,
  115.        gs_memory_t * mem)
  116. {
  117.     i_ctx_t *i_ctx_p = (i_ctx_t *)iodev->state;    /* see above */
  118.     stream *s;
  119.  
  120.     if (!streq1(access, 'r'))
  121.     return_error(e_invalidfileaccess);
  122.     if (file_is_invalid(s, &ref_stdin)) {
  123.     /****** stdin SHOULD NOT LINE-BUFFER ******/
  124.     gs_memory_t *mem = imemory_system;
  125.     byte *buf;
  126.  
  127.     s = file_alloc_stream(mem, "stdin_open(stream)");
  128.     /* We want stdin to read only one character at a time, */
  129.     /* but it must have a substantial buffer, in case it is used */
  130.     /* by a stream that requires more than one input byte */
  131.     /* to make progress. */
  132.     buf = gs_alloc_bytes(mem, STDIN_BUF_SIZE, "stdin_open(buffer)");
  133.     if (s == 0 || buf == 0)
  134.         return_error(e_VMerror);
  135.     sread_file(s, gs_stdin, buf, STDIN_BUF_SIZE);
  136.     s->procs.process = s_stdin_read_process;
  137.     s->save_close = s_std_null;
  138.     s->procs.close = file_close_file;
  139.     make_file(&ref_stdin, a_readonly | avm_system, s->read_id, s);
  140.     *ps = s;
  141.     return 1;
  142.     }
  143.     *ps = s;
  144.     return 0;
  145. }
  146. /* This is the public routine for getting the stdin stream. */
  147. int
  148. zget_stdin(i_ctx_t *i_ctx_p, stream ** ps)
  149. {
  150.     stream *s;
  151.     gx_io_device *iodev;
  152.     int code;
  153.  
  154.     if (file_is_valid(s, &ref_stdin)) {
  155.     *ps = s;
  156.     return 0;
  157.     }
  158.     iodev = gs_findiodevice((const byte *)"%stdin", 6);
  159.     iodev->state = i_ctx_p;
  160.     code = (*iodev->procs.open_device)(iodev, "r", ps, imemory_system);
  161.     iodev->state = NULL;
  162.     return min(code, 0);
  163. }
  164. /* Test whether a stream is stdin. */
  165. bool
  166. zis_stdin(const stream *s)
  167. {
  168.     return (s_is_valid(s) && s->procs.process == s_stdin_read_process);
  169. }
  170.  
  171. private int
  172. stdout_open(gx_io_device * iodev, const char *access, stream ** ps,
  173.         gs_memory_t * mem)
  174. {
  175.     i_ctx_t *i_ctx_p = (i_ctx_t *)iodev->state;    /* see above */
  176.     stream *s;
  177.  
  178.     if (!streq1(access, 'w'))
  179.     return_error(e_invalidfileaccess);
  180.     if (file_is_invalid(s, &ref_stdout)) {
  181.     gs_memory_t *mem = imemory_system;
  182.     byte *buf;
  183.  
  184.     s = file_alloc_stream(mem, "stdout_open(stream)");
  185.     buf = gs_alloc_bytes(mem, STDOUT_BUF_SIZE, "stdout_open(buffer)");
  186.     if (s == 0 || buf == 0)
  187.         return_error(e_VMerror);
  188.     swrite_file(s, gs_stdout, buf, STDOUT_BUF_SIZE);
  189.     s->save_close = s->procs.flush;
  190.     s->procs.close = file_close_file;
  191.     make_file(&ref_stdout, a_write | avm_system, s->write_id, s);
  192.     *ps = s;
  193.     return 1;
  194.     }
  195.     *ps = s;
  196.     return 0;
  197. }
  198. /* This is the public routine for getting the stdout stream. */
  199. int
  200. zget_stdout(i_ctx_t *i_ctx_p, stream ** ps)
  201. {
  202.     stream *s;
  203.     gx_io_device *iodev;
  204.     int code;
  205.  
  206.     if (file_is_valid(s, &ref_stdout)) {
  207.     *ps = s;
  208.     return 0;
  209.     }
  210.     iodev = gs_findiodevice((const byte *)"%stdout", 7);
  211.     iodev->state = i_ctx_p;
  212.     code = (*iodev->procs.open_device)(iodev, "w", ps, imemory_system);
  213.     iodev->state = NULL;
  214.     return min(code, 0);
  215. }
  216.  
  217. private int
  218. stderr_open(gx_io_device * iodev, const char *access, stream ** ps,
  219.         gs_memory_t * mem)
  220. {
  221.     i_ctx_t *i_ctx_p = (i_ctx_t *)iodev->state;    /* see above */
  222.     stream *s;
  223.  
  224.     if (!streq1(access, 'w'))
  225.     return_error(e_invalidfileaccess);
  226.     if (file_is_invalid(s, &ref_stderr)) {
  227.     gs_memory_t *mem = imemory_system;
  228.     byte *buf;
  229.  
  230.     s = file_alloc_stream(mem, "stderr_open(stream)");
  231.     buf = gs_alloc_bytes(mem, STDERR_BUF_SIZE, "stderr_open(buffer)");
  232.     if (s == 0 || buf == 0)
  233.         return_error(e_VMerror);
  234.     swrite_file(s, gs_stderr, buf, STDERR_BUF_SIZE);
  235.     s->save_close = s->procs.flush;
  236.     s->procs.close = file_close_file;
  237.     make_file(&ref_stderr, a_write | avm_system, s->write_id, s);
  238.     *ps = s;
  239.     return 1;
  240.     }
  241.     *ps = s;
  242.     return 0;
  243. }
  244. /* This is the public routine for getting the stderr stream. */
  245. int
  246. zget_stderr(i_ctx_t *i_ctx_p, stream ** ps)
  247. {
  248.     stream *s;
  249.     gx_io_device *iodev;
  250.     int code;
  251.  
  252.     if (file_is_valid(s, &ref_stderr)) {
  253.     *ps = s;
  254.     return 0;
  255.     }
  256.     iodev = gs_findiodevice((const byte *)"%stderr", 7);
  257.     iodev->state = i_ctx_p;
  258.     code = (*iodev->procs.open_device)(iodev, "w", ps, imemory_system);
  259.     iodev->state = NULL;
  260.     return min(code, 0);
  261. }
  262.